﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SoftwareClass_1
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string insertuid = textBox1.Text;
            string inserpwd = textBox2.Text;
            string repwd = textBox3.Text;
            bool hasuser = !string.IsNullOrEmpty(insertuid);
            bool haspwd = !string.IsNullOrEmpty(inserpwd);

            var sql = string.Format("select COUNT(*) from Users where Username = '{0}'", insertuid);
            string comString = "server =PC-202001311751\\MYSQLSERVER; uid =yun0823;pwd =jxy130452;database =Blogs";
            SqlConnection sqlConnection = new SqlConnection(comString);
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
            sqlConnection.Open();
            int i = Convert.ToInt32(sqlCommand.ExecuteScalar());
            sqlConnection.Close();
            if (i > 0)
            {
                MessageBox.Show("已存在该用户名");
            }
            else
            {

                if (!hasuser)
                {
                    MessageBox.Show("用户名不能为空");
                }
                else
                {
                    if (!haspwd)
                    {
                        MessageBox.Show("密码不能为空");
                    }
                    else if (inserpwd != repwd && !haspwd)
                    {
                        MessageBox.Show("密码不相同");
                    }
                    else
                    {
                        var str = string.Format("insert into Users (Username,[Password]) values ('{0}','{1}')", insertuid, inserpwd);
                        DBhelper.GetDataTable(str);
                        MessageBox.Show("注册成功");
                        this.Close();
                    }
                }
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
